﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchEdu.Framework.Logging;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;
using SchEdu.Framework.Validators;

namespace SchEdu.Framework.Objective
{
    public abstract class Objective : IObjective
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        SchEduData schEduData;

        public SchEduData SchEduData
        {
            get { return schEduData; }
            set { schEduData = value; }
        }

        SchEduDataSet.SolutionRow solution;

        public SchEduDataSet.SolutionRow Solution
        {
            get { return solution; }
            set { solution = value; }
        }

        protected SchEduDataSet.PerformanceRow performance;

        protected ValidatorList validators = new ValidatorList();


        public Objective()
        {
        }

        public Objective(SchEduData schEduData,SchEduDataSet.SolutionRow solution)
        {
            this.schEduData = schEduData;
            this.solution = solution;
        }

        protected abstract void Logic();

        #region IObjective Members

        public virtual void Run()
        {
            Initialize();

            Validate();

            Logic();

        }

        private void Initialize()
        {
            InitializeValidators();
        }

        protected virtual void InitializeValidators()
        {
            validators = new ValidatorList(); 
            validators.Add(new AllocationValidator(schEduData,solution));
        }

        protected virtual void Validate()
        {
            foreach (IValidator validator in validators)
            {
                validator.Validate();
            }
        }

        public abstract AboutInfo AboutInfo
        {
            get;
        }

        protected void GeneratePerformanceRow()
        {
            performance = schEduData.Performance.NewPerformanceRow();
            performance.SolutionID = solution.ID;
            performance.ObjectiveGuid = AboutInfo.Guid;
            performance.MachineGuid = System.Guid.Empty;
            performance.Title = AboutInfo.Title;
            performance.Version = AboutInfo.Version;
            performance.Company = AboutInfo.Company;
            performance.Copyright = AboutInfo.Copyright;
            performance.Description = AboutInfo.Description;
            performance.Product = AboutInfo.Product;
            schEduData.Performance.AddPerformanceRow(performance);
        }


        #endregion
    }
}
